#include "spl.includes.h"
#include "splib.h"

PTCHAR SPLib::Settings::copyParam(PTSTR szTarget, PTSTR szSource)
{
    // copy source to target until ',' or '\r' is found
    while (*szSource != ',' && *szSource != '\r')
        *(szTarget++) = *(szSource++);

    // append null terminating character to target
    *szTarget = '\0';

    // return last processed char
    return szSource;
}

PTCHAR SPLib::Settings::copyString(PTSTR szTarget, PCTSTR szSource)
{
    // copy source to target until ',' or '\r' is found
    while (*szSource != '\0') *(szTarget++) = *(szSource++);

    // append null terminating character to target
    *szTarget = '\0';

    // return last processed char
    return szTarget;
}

BOOL SPLib::Settings::compareParam(PTSTR szSource, PCTSTR szText, PTCHAR *pLast)
{
    // while both strings end are not reached and both characters match
    while (*szSource != ',' && *szSource != '\0' &&
        szText != '\0' && *szSource == *szText) {
        // increment pointers
        ++szSource;
        ++szText;
    }

    // set last processed char
    *pLast = szSource;

    // check if both strings matched
    return (*szSource == ',' || *szSource == '\0') && *szText == '\0';
}

void SPLib::Settings::stringFormat(PTSTR szOut, PCTSTR szIn, PCTSTR szEnd)
{
    // special character flag
    BOOL fSpecial = FALSE;

    // while there are still input characters remaining
    while (szIn < szEnd) {
        // if processing special character
        if (fSpecial) {
            switch (*szIn) {
                case '\\':
                    // append counterslash
                    *szOut = '\\';
                    break;
                case 'n':
                    // append line feed
                    *szOut = '\n';
                    break;
                case 'r':
                    // append carriage return
                    *szOut = '\r';
                    break;
                default:
                    // no special behaviour
                    // do a simple copy from input to output
                    *szOut = *szIn;
                    break;
            }

            // increment output char
            ++szOut;
            // unset special character flag
            fSpecial = FALSE;
        } else if (*szIn == '\\') {
            // set special character flag
            fSpecial = TRUE;
        } else {
            // copy input char to output
            // and increment output char
            *(szOut++) = *szIn;
        }

        // increment input char
        ++szIn;
    }

    // append null terminating character
    *szOut = '\0';
}